<template>
  <view class="p-h24 p-t40" style="padding-bottom: 208rpx">
    <ResumePreview v-if="resume" :resume="resume" />
    <view class="p-h24 flex-sb bg-1 foot">
      <view class="flex-1 flex">
        <view class="flex-d-c action" @click="handleCollect">
          <image v-if="resume.sc" class="icon" src="@/static/images/public/uncollect.png" />
          <image v-else class="icon" src="@/static/images/public/collect.png" />
          <text class="text-400-128">{{ resume.sc ? "已收藏" : "收藏" }}</text>
        </view>
        <view class="flex-d-c action" @click="handleRejectResume">
          <image v-if="resume.td_status === 2" class="icon" src="@/static/images/public/uncollect.png" />
          <image v-else class="icon" src="@/static/images/public/collect.png" />
          <text class="text-400-128">不合适</text>
        </view>
      </view>
      <RcButton :width="192" :text="td_id ? '通知面试' : '邀请面试'" :top="0" :size="28" @click="handleOpenNotify" />
      <RcButton class="m-l20" :width="192" text="入职" :top="0" :size="28" v-if="mianshi.results === 0"
        @click="handleAdd(mianshi.id)" />
      <RcButton class="m-l20" :width="192" text="离职" :top="0" :size="28" v-if="mianshi.results === 1"
        @click="handleOut(mianshi.id)" />
    </view>
    <up-popup :show="interviewNoticeVisible" mode="bottom" closeOnClickOverlay @close="interviewNoticeVisible = false">
      <InterviewNotice :user_id="user_id" :id="td_id" @close="handleClose" />
    </up-popup>
  </view>
</template>

<script lang="ts" setup>
import { ref } from "vue";
import RcButton from "@/components/button.vue";
import InterviewNotice from "./components/interview-notice";
import type { TResume } from "@/types/api.d";
import ResumePreview from "@/components/resume-preview";
import { onLoad, onShow, onShareAppMessage, onShareTimeline } from "@dcloudio/uni-app";
onShareAppMessage(() => {
  return {
		title:resume.value.nickname+'个人简历，欢迎查看',
    path: user_id.value ? `/pages/office/resume/resume?user_id=${user_id.value}` : `/pages/office/resume/resume?id=${td_id.value}`,
  }
})
onShareTimeline(() => {
  return {
		title:resume.value.nickname+'个人简历，欢迎查看',
    path: user_id.value ? `/pages/office/resume/resume?user_id=${user_id.value}` : `/pages/office/resume/resume?id=${td_id.value}`,
  }
})
const user_id = ref("");
const td_id = ref("");
const resume = ref<TResume>({});
const interviewNoticeVisible = ref<boolean>(false);
const mianshi = ref({});
//打开弹窗
function handleOpenNotify() {
  // if (!td_id.value) {
  //   return uni.showToast({ title: "请在“收到的简历投递”里进行通知面试", icon: "none" })
  // }
  if (resume.value.td_status == 1) {
    return uni.showToast({ title: "已邀请面试", icon: "none" });
  }
  if (resume.value.td_status == 1) {
    return uni.showToast({ title: "不合适", icon: "none" });
  }
  if (resume.value.td_status == 0) {
    interviewNoticeVisible.value = true;
  }
}
//关闭弹窗
function handleClose() {
  interviewNoticeVisible.value = false;
  getInfo();
}
const handleCollect = () => {
  uni.showLoading({ mask: true });
  if (resume.value.sc) {
    uni.$u
      .post("/resume/delsc", { user_id: user_id.value })
      .then((res) => {
        uni.$u.toast(res.msg);
        if (res.code === 1) {
          // 更新简历信息
          getInfo();
        }
      })
      .finally(() => {
        uni.hideLoading();
      });
  } else {
    uni.$u
      .post("/resume/addsc", { user_id: user_id.value })
      .then((res) => {
        uni.$u.toast(res.msg);
        if (res.code === 1) {
          // 更新简历信息
          getInfo();
        }
      })
      .finally(() => {
        uni.hideLoading();
      });
  }
};
//标记不合适
const handleRejectResume = () => {
  uni.showModal({
    title: "提示",
    content: "你确定要标记不合适吗",
    success: async function (res) {
      if (res.confirm) {
        uni.showLoading({ mask: true });
        uni.$u
          .post("/resume/nojl", { user_id: user_id.value })
          .then((res) => {
            if (res.code === 1) {
              uni.$u.toast("标记成功");
              // 更新简历信息
              getInfo();
            }
          })
          .finally(() => {
            uni.hideLoading();
          });
      } else if (res.cancel) {
        console.log("取消");
      }
    },
  });
};

//获取个人信息
const getInfo = async () => {
  const res = await uni.$u.get("/resume/resume_info", { id: user_id.value });
  resume.value = res.data;
  if (res.data.mianshi.length) {
    mianshi.value = res.data.mianshi[res.data.mianshi.length - 1];
  }
};

//初始化
onLoad((e) => {
  user_id.value = e.user_id;
  td_id.value = e.id;
  getInfo();
});

//入职
async function handleAdd(id: number) {
  uni.showModal({
    title: "提示",
    content: "确定入职吗？",
    showCancel: true,
    cancelText: "取消",
    cancelColor: "#000000",
    confirmText: "确定",
    confirmColor: "#3CC51F",
    success: async (result) => {
      if (result.confirm) {
        const res = await uni.$u.post("/resume/entry", { id });
        if (res.code == 1) {
          uni.showToast({ title: "入职成功" });
          getInfo();
        } else {
          uni.showToast({ title: res.msg });
        }
      }
    },
  });
}
//离职
async function handleOut(id: number) {
  uni.showModal({
    title: "提示",
    content: "确定离职吗？",
    showCancel: true,
    cancelText: "取消",
    cancelColor: "#000000",
    confirmText: "确定",
    confirmColor: "#3CC51F",
    success: async (result) => {
      if (result.confirm) {
        const res = await uni.$u.post("/resume/resignation", { id });
        if (res.code == 1) {
          uni.showToast({ title: "操作成功" });
          getInfo();
        } else {
          uni.showToast({ title: res.msg });
        }
      }
    },
  });
}
</script>

<style lang="scss">
page {
  background-color: $page-color-4;
}
</style>

<style lang="scss" scoped>
.foot {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 168rpx;

  .action {
    width: 100rpx;
  }

  .icon {
    width: 48rpx;
    height: 48rpx;
  }
}
</style>
